package com.yifi.system.controller;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.base.BaseController;
import com.yifi.common.model.MenuModel;
import com.yifi.common.utils.DbUtils;
import com.yifi.common.utils.ToolSession;
import com.yifi.common.utils.ToolString;
import com.yifi.system.service.MenuService;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

/** 
* @ClassName: SysMenuController 
* @Description: TODO
* @author 姚善利
* @date 2021-10-19 9:38:43 
*  
*/

@Path(value="/sysmenu",viewPath = "/")
public class SysMenuController extends BaseController {

	@Inject
	private MenuService menuService;
	
	public void index()
	{
		render("system/sysmenu.html");
	}
	
	public void menuTree()
	{
		renderJson(menuService.GetMenuTree());
	}
	
	public void getDataTree()
	{
		String ccode=getPara("ccode","");
		String sql="select * from work_class2 where ccode='"+ccode+"' ";
		Record record=Db.findFirst(sql);
		renderJson(record);
	}
	
	public void saveTree()
	{
		String ccode=getPara("ccode","");
		String pclass=getPara("pclass","0");
		String cname=getPara("cname","");
		String isort=getPara("isort","1");

		String sql="select * from work_class2 where ccode='"+ccode+"' ";
		Record record=Db.findFirst(sql);
		boolean isedit=true;
		if(record==null)
		{
			isedit=false;
			record = new Record();
			record.set("ccode", ccode);
			record.set("class1", "001");
			record.set("bc_flag", "bscs");
			record.set("user_flag", "1");
			record.set("st_flag", "1");
		}
		int ilayer=1;
		if(!pclass.equals("0"))
		{
			ilayer=2;
		}
		record.set("pclass", pclass);
		record.set("cname", cname);
		record.set("ilayer", ilayer);
		record.set("isort", isort);
		
		if (DbUtils.save("work_class2", "ccode", record,isedit)) 
		{
			renderJson(success("保存成功！"));
		}
		else
		{
			renderJson(error("保存失败！"));
		}
	}
	public void deleteTree()
	{
		String ccode=getPara("ccode","");
		
		String sql="select count(1) from work where cclass2='"+ccode+"'";
		if(Db.queryInt(sql)>0)
		{
			renderJson(error("存在菜单，不能删除"));
			return;
		}
		sql="select count(1) from work_class2 where pclass='"+ccode+"'";
		if(Db.queryInt(sql)>0)
		{
			renderJson(error("存在子目录，不能删除"));
			return;
		}
		
		sql="delete from work_class2 where ccode='"+ccode+"'";
		if(Db.update(sql)>0)
		{
			renderJson(success("删除成功！"));
		}
		else
		{
			renderJson(error("删除失败！"));
		}
		
	}
	
	public void list()
	{
		String cclass2=getPara("cclass2","");
		
		String sqlSelect="select * ";
		String sqlExcept=" from work where 1=1 ";
		if(StrUtil.isNotEmpty(cclass2))
		{
			sqlExcept+=" and cclass2='"+cclass2+"' ";
		}
		
		sqlExcept+=" order by isort";
		
		renderJson(getDataTable(sqlSelect, sqlExcept));
	}
	
	public void getData()
	{
		String ccode=getPara("ccode","");
		String sql="select t1.*,(select cname from work_class2 where ccode=t1.cclass2) as cclass2name"
				+ " from work t1 where t1.ccode='"+ccode+"' ";
		Record record=Db.findFirst(sql);
		renderJson(record);
	}
	
	public void edit()
	{
		String ccode=getPara("ccode","");
		set("ccode", ccode);
		
		String cclass2=getPara("cclass2","");
		set("cclass2", cclass2);
		
		render("system/sysmenu_edit.html");
	}
	
	public void save()
	{
		String ccode = getPara("ccode","");
		String sql="select * from work where ccode='"+ccode+"'";
		Record record=Db.findFirst(sql);
		boolean isedit=true;
		if(record==null)
		{
			isedit=false;
			record = new Record();
			record.set("ccode", IdUtil.fastUUID());
			record.set("bc_flag", "bscs");
			record.set("cwk_type", "2");
			record.set("cclass1", "001");
		}
		
		String st_flag = getPara("st_flag", "0");
		String ctarger = getPara("ctarger", "1");
		String cname=getPara("cname","");
		String cclass2=getPara("cclass2","");
		String cdesc=getPara("cdesc","");
		String cprg=getPara("cprg","");
		String cprg_cs=getPara("cprg_cs","");
		String isort=getPara("isort","1");
		
		record.set("cname", cname);
		record.set("cclass2", cclass2);
		record.set("cdesc", cdesc);
		record.set("cprg", cprg);
		record.set("cprg_cs", cprg_cs);
		record.set("isort", isort);
		record.set("ctarger", ctarger);
		record.set("st_flag", st_flag);

		if (DbUtils.save("work", "ccode", record,isedit)) 
		{
			renderJson(success("保存成功！"));
		}
		else
		{
			renderJson(error("保存失败！"));
		}
	}
	
	public void remove()
	{
		String ccode = getPara("ids","");
		String sql="delete from work where ccode='"+ccode+"'";
		if(Db.update(sql)>0)
		{
			renderJson(success("删除成功！"));
		}
		else
		{
			renderJson(error("删除失败！"));
		}
	}
	
	//系统font图标
	public void icon()
	{
		render("system/icon.html");
	}
	
	public void select()
	{
		render("system/sysmenu_select.html");
	}
	
	public void getMenuTree()
	{
		String sql="select ccode,cname,pclass from work_class2 order by isort";
		
		List<Record> list=Db.find(sql);
		renderJson(list);
	}
	
	//菜单权限
	public void auth()
	{
		String c_pro = ToolSession.getUser(this).getC_pro();
		set("c_pro", c_pro);
		render("system/sysmenu_auth.html");
	}
	
	public void authtree()
	{
		String cpost=getPara("cpost","");
		
		List<MenuModel> listMenu = menuService.getParent(menuService.getMenuPost(cpost));
		
		renderJson(listMenu);
	}
	
	public void addtree()
	{
		String menuIds=getPara("menuIds","");
		String cpost=getPara("cpost","");
		String[] arrId=menuIds.split(",");
		String sql="delete from mis_post_menu where cpost='"+cpost+"'";
		Db.update(sql);
		List<String> listSql=new ArrayList<String>();
    	for (String str : arrId) {
    		sql="select count(1) from work where ccode='"+str+"'";
    		if(Db.queryInt(sql)>0)
    		{
    			sql="insert mis_post_menu (cwork,cpost,cflag) values ('"+str+"','"+cpost+"','1')";
    			listSql.add(sql);
    		}
		}
		if(Db.batch(listSql, listSql.size()).length>-1)
		{
			renderJson(success("保存成功！"));
		}
		else
		{
			renderJson(error("保存失败！"));
		}
    	
	}
}
